<?php
declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadController extends Controller
{
    public function upload(Request $request)
    {
        $result = ['errno' =>1, 'message'=>'上传失败！'];
        if ($request->isMethod('POST')) {
            if ($request->hasFile('upload_name')) {
                $fileName = $request->file('upload_name');
                if ($fileName->isValid()) {
                    $fileType = $request->input('upload_type');
                    $newFileName = md5($fileName->getClientOriginalName() . time() . mt_rand(1, 10000)) . '.' . $fileName->getClientOriginalExtension();
                    if ($fileType == 'image') {
                        $savePath = 'images/' . date('Ymd', time());
                    } elseif ($fileType == 'file') {
                        $savePath = 'files/' . date('Ymd', time());
                    } elseif ($fileType == 'video') {
                        $savePath = 'videos/' . date('Ymd', time()) ;
                    } else {
                        $savePath = 'others/' . date('Ymd', time());
                    }
                    if ($fileName->storePubliclyAs($savePath, $newFileName, ['disk' => 'public'])) {
                        $result['errno'] = 0;
                        $result['message'] = '上传成功！';
                        $result['data'] = [
                            'url' =>  Storage::url($savePath . '/' . $newFileName),
                        ];
                        return response()->json($result);
                    }
                }else{
                    $result['message'] = "不被允许的上传文件！";
                    return response()->json($result);
                }
            }else{
                $result['message'] = "请选择要上传的文件！";
                return response()->json($result);
            }
        }
        return response()->json($result);
    }

    public function image(Request $request)
    {
        $result = ['errno' =>1, 'message'=>'上传失败！'];
        if ($request->isMethod('POST')) {
            if ($request->hasFile('upload_name')) {
                $fileName = $request->file('upload_name');
                if ($fileName->isValid()) {
                    $newFileName = md5($fileName->getClientOriginalName() . time() . mt_rand(1, 10000)) . '.' . $fileName->getClientOriginalExtension();
                    $savePath = 'images/' . date('Ymd', time());
                    if ($fileName->storePubliclyAs($savePath, $newFileName, ['disk' => 'public'])) {
                        $result['errno'] = 0;
                        $result['message'] = '上传成功！';
                        $result['data'] = [
                            'url' =>  Storage::url($savePath . '/' . $newFileName),
                        ];
                        return response()->json($result);
                    }
                }else{
                    $result['message'] = "不被允许的上传文件！";
                    return response()->json($result);
                }
            }else{
                $result['message'] = "请选择要上传的文件！";
                return response()->json($result);
            }
        }
        return response()->json($result);
    }

    public function video(Request $request)
    {
        $result = ['errno' =>1, 'message'=>'上传失败！'];
        if ($request->isMethod('POST')) {
            if ($request->hasFile('upload_name')) {
                $fileName = $request->file('upload_name');
                if ($fileName->isValid()) {
                    $newFileName = md5($fileName->getClientOriginalName() . time() . mt_rand(1, 10000)) . '.' . $fileName->getClientOriginalExtension();
                    $savePath = 'videos/' . date('Ymd', time());
                    if ($fileName->storePubliclyAs($savePath, $newFileName, ['disk' => 'public'])) {
                        $result['errno'] = 0;
                        $result['message'] = '上传成功！';
                        $result['data'] = [
                            'url' =>  Storage::url($savePath . '/' . $newFileName),
                        ];
                        return response()->json($result);
                    }
                }else{
                    $result['message'] = "不被允许的上传文件！";
                    return response()->json($result);
                }
            }else{
                $result['message'] = "请选择要上传的文件！";
                return response()->json($result);
            }
        }
        return response()->json($result);
    }
}
